package com.kifas.controller;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;
import javax.xml.bind.JAXBElement;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.kifas.entity.GudangEntity;
import com.kifas.model.xml.GudangModel;
import com.kifas.model.xml.PersistStatus;
import com.kifas.service.EntityServiceManager;

/**
 * Created by IntelliJ IDEA. User: brainmaster Date: 4/27/11 Time: 12:18 AM To
 * change this template use File | Settings | File Templates.
 */
@Component
@Scope("prototype")
@Path("/Gudangs")
public class GudangController {

	private static Logger log = Logger.getLogger(GudangController.class);

	@Context
	UriInfo uriInfo;

	@Autowired
	private EntityServiceManager entityManager;

	@GET
	@Produces(MediaType.TEXT_XML)
	public List<GudangModel> getGudangs() {
		List<GudangEntity> gudangEntities = entityManager.getGudangs();
		List<GudangModel> gudangs = new ArrayList<GudangModel>();
		for (GudangEntity gudangEntity : gudangEntities) {
			GudangModel gudang = new GudangModel(gudangEntity.getIdGudang(), gudangEntity.getNamaGudang(), gudangEntity.getLokasi(),
					gudangEntity.getKapasitas(), gudangEntity.getPetugas(), gudangEntity.getAlamat(), gudangEntity.getKode(), gudangEntity.getNotelp(),
					gudangEntity.getTgl_jth_tempo(), gudangEntity.getCreatedBy());
			gudangs.add(gudang);
		}
		return gudangs;
	}

	@GET
	@Path("{id}")
	@Produces(MediaType.TEXT_XML)
	public GudangModel getGudang(@PathParam("id") Long id) {
		GudangEntity gudangEntity = entityManager.getGudang(id);
		return new GudangModel(gudangEntity.getIdGudang(), gudangEntity.getNamaGudang(), gudangEntity.getLokasi(), gudangEntity.getKapasitas(),
				gudangEntity.getPetugas(), gudangEntity.getAlamat(), gudangEntity.getKode(), gudangEntity.getNotelp(), gudangEntity.getTgl_jth_tempo(),
				gudangEntity.getCreatedBy());
	}

    @POST
    @Produces(MediaType.APPLICATION_XML)
    @Consumes(MediaType.APPLICATION_XML)
    public PersistStatus persist(JAXBElement<GudangModel> gudangXml) {
        GudangModel model = gudangXml.getValue();
        GudangEntity gudangEntity = new GudangEntity();
        gudangEntity.setIdGudang(null);
        gudangEntity.setNamaGudang(model.getNamaGudang());
        gudangEntity.setLokasi(model.getLokasi());
        gudangEntity.setAlamat(model.getAlamat());
        gudangEntity.setKapasitas(model.getKapasitas());
        gudangEntity.setKode(model.getKode());
        gudangEntity.setPetugas(model.getPetugas());
        gudangEntity.setTgl_jth_tempo(model.getTgl_jth_tempo());
        gudangEntity.setNotelp(model.getNotelp());
        try {
            entityManager.persist(gudangEntity);
        } catch (Exception e) {
        	log.error("can not persist Gudang Entity", e);
            return new PersistStatus("GudangEntity", "failed", "can not persist gudang entity");
        }
        return new PersistStatus("GudangEntity", "success", "success save gudang data...");
    }
    
    @PUT
    @Produces(MediaType.APPLICATION_XML)
    @Consumes(MediaType.APPLICATION_XML)
    public PersistStatus update(JAXBElement<GudangModel> xml) {
    	GudangModel model = xml.getValue();
        GudangEntity gudangEntity = new GudangEntity();
        gudangEntity.setIdGudang(model.getIdMasterGudang());
        gudangEntity.setNamaGudang(model.getNamaGudang());
        gudangEntity.setLokasi(model.getLokasi());
        gudangEntity.setAlamat(model.getAlamat());
        gudangEntity.setKapasitas(model.getKapasitas());
        gudangEntity.setKode(model.getKode());
        gudangEntity.setPetugas(model.getPetugas());
        gudangEntity.setTgl_jth_tempo(model.getTgl_jth_tempo());
        gudangEntity.setNotelp(model.getNotelp());
    	try {
                entityManager.update(gudangEntity);
        } catch (Exception e) {
                log.error("can not update Gudang Entity", e);
                return new PersistStatus("GudangEntity", "failed", "can not update Gudang Entity");
        }
    	return new PersistStatus("GudangEntity", "success", "success save gudang data...");
    }
}
